﻿using AutoMapper;
using DFGK_WMS.Context;
using DFGK_WMS.IRepository;
using DFGK_WMS.Model.DTO;
using DFGK_WMS.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DFGK_WMS.Repository
{
    public class TInstructRepository : BaseRepository<TInstruct>, ITInstructRepository
    {
        public TInstructRepository(DFGK_DB context, IMapper mapper) : base(context, mapper)
        {
        }

        public List<TInstructDto> GetTInstructPage(out int totalCount,int? InstructType, int? Status, string? PalletBarcode, int pageIndex = 1, int pageSize = 10)
        {
            var query = Context.TInstructs.AsQueryable();

            // 添加条件判断
            if (InstructType.HasValue)
            {
                query = query.Where(t => t.InstructType == InstructType.Value);
            }

            if (Status.HasValue)
            {
                query = query.Where(t => t.Status == Status.Value);
            }

            if (!string.IsNullOrEmpty(PalletBarcode))
            {
                query = query.Where(t => t.PalletBarcode.Contains(PalletBarcode));
            }

            // 计算总条数
            totalCount = query.Count();

            // 分页
            var result = query.Skip((pageIndex - 1) * pageSize)
                              .Take(pageSize)
                              .ToList()
                              .Select(s => new TInstructDto()
                              {
                                  Id = s.Id,
                                  WarehouseCode = s.WarehouseCode,
                                  StackingStorageId = s.StackingStorageId,
                                  Status = GetStatusText(s.Status),
                                  InstructType = GetInstructTypeText(s.InstructType),
                                  WorkDeviceType = s.WorkDeviceType,
                                  Action = s.Action,
                                  Priority = s.Priority,
                                  FromLocation = s.FromLocation,
                                  ExpectedToLocation = s.ExpectedToLocation,
                                  ToLocation = s.ToLocation,
                                  Qty = s.Qty,
                                  FinishedQty = s.FinishedQty,
                                  PalletBarcode = s.PalletBarcode,
                                  ScanedBarcode = s.ScanedBarcode,
                                  InstructIndex = s.InstructIndex,
                                  StartTime = s.StartTime,
                                  EndTime = s.EndTime,
                                  IsSendSuccess = s.IsSendSuccess,
                                  RelatedOrderId = s.RelatedOrderId,
                                  RelatedOrderDetailId = s.RelatedOrderDetailId,
                                  DeviceCode = s.DeviceCode,
                                  FromRoutewayId = s.FromRoutewayId,
                                  BusinessType = s.BusinessType,
                                  BusinessGroupId = s.BusinessGroupId,
                                  BillType = s.BillType,
                                  RefCellCode = s.RefCellCode,
                                  IsSuspend = s.IsSuspend,
                                  LogicalCellAddr = s.LogicalCellAddr,
                                  CreateUserId = s.CreateUserId,
                                  CreateUserName = s.CreateUserName
                              })
                              .ToList();

            return result;
        }
        private string GetStatusText(int status)
        {
            string statusText;
            switch (status)
            {
                case 0:
                    statusText = "创建";
                    break;
                case 1:
                    statusText = "待执行";
                    break;
                case 2:
                    statusText = "执行中";
                    break;
                case 3:
                    statusText = "搬运完成";
                    break;
                case 4:
                    statusText = "处理完成";
                    break;
                case 5:
                    statusText = "执行异常";
                    break;
                case 6:
                    statusText = "已强制取消任务";
                    break;
                default:
                    statusText = "未知状态";
                    break;
            }

            return statusText;
        }
        private string GetInstructTypeText(int instructType)
        {
            string instructTypeText;
            switch (instructType)
            {
                case 1:
                    instructTypeText = "上架";
                    break;
                case 2:
                    instructTypeText = "下架";
                    break;
                case 3:
                    instructTypeText = "移位";
                    break;
                case 4:
                    instructTypeText = "盘点";
                    break;
                default:
                    instructTypeText = "未知指令类型";
                    break;
            }

            return instructTypeText;
        }

        public bool DelTInstruct(int id)
        {
            try
            {
                var instruct = Context.TInstructs.FirstOrDefault(x => x.Id == id);
                if (instruct == null)
                {
                    throw new Exception("找不到该数据");
                }
                Context.Remove(instruct);
                return Context.SaveChanges() > 0;
            }
            catch (Exception)
            {

                throw;
            }

        }

        public bool UpdStatus(int id, out string msg)
        {
            try
            {
                var obj=Context.TInstructs.FirstOrDefault(s=>s.Id == id);
                if (obj == null)
                {
                    msg = "找不到此id对应的数据";
                    return false;
                }
                //该指令操作的货物
                var huoCount = Context.TInventoryDetails.Where(s=>s.PalletBarcode==obj.PalletBarcode).Count();
                if (huoCount <= 0)
                {
                    msg = "库存明细数据不存在";
                    return false;
                }
                msg = "成功";
                obj.Status = 3;
                Context.TInstructs.Update(obj);
                return Context.SaveChanges() > 0;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
